package com.frank.activedatabus.Core;

import com.frank.activedatabus.dispatch.ThreadMode;
import ohos.aafwk.ability.Lifecycle;

class ObservableConfig {

    boolean isAlways;

    boolean isSticky;

    Lifecycle mLifecycle;

    Boolean autoClear = null;

    private ThreadMode threadMode = null;

    ThreadMode getThreadMode() {
        return threadMode;
    }

    public ObservableConfig() {

    }

    /**
     * isAlways
     * set if then observer can always receive message
     * true: observer can always receive message
     * false: observer can only receive message when resumed
     *
     * @param isAlways
     * @return
     */
    public ObservableConfig isAlways(boolean isAlways) {
        this.isAlways = isAlways;
        return this;
    }

    public ObservableConfig isAutoClear(boolean isAutoClear) {
        this.autoClear = isAutoClear;
        return this;
    }

    public ObservableConfig setThreadMode(ThreadMode threadMode) {
        this.threadMode = threadMode;
        return this;
    }

    private ObservableConfig(boolean isAlways, boolean isSticky, Lifecycle mLifecycle, ThreadMode threadMode) {
        this.isAlways = isAlways;
        this.isSticky = isSticky;
        this.mLifecycle = mLifecycle;
        this.threadMode = threadMode;
    }

    public static class ObservableConfigBuilder {

        boolean isAlways = false;

        boolean isSticky = false;

        private Lifecycle mLifecycle;

        private ThreadMode threadMode = ThreadMode.DEFAILT;

        Boolean autoClear = null;

        private ObservableConfigBuilder() {
        }

        public ObservableConfigBuilder withAlways(boolean always) {
            this.isAlways = always;
            return this;
        }

        public ObservableConfigBuilder withSticky(boolean sticky) {
            this.isSticky = sticky;
            return this;
        }

        public ObservableConfigBuilder withLifeCycle(Lifecycle lifeCycle) {
            this.mLifecycle = lifeCycle;
            return this;
        }

        public ObservableConfigBuilder withThreadModel(ThreadMode mode) {
            this.threadMode = mode;
            return this;
        }

        public Lifecycle getLifecycle() {
            if (mLifecycle == null) {
                mLifecycle = new Lifecycle();
            }
            return mLifecycle;
        }

        public ObservableConfigBuilder autoClear(Boolean autoClear) {
            this.autoClear = autoClear;
            return this;
        }

        public ObservableConfig build() {
            return new ObservableConfig(isAlways, isSticky, getLifecycle(), threadMode);
        }
    }
}
